package com.mall4j.cloud.product.service.impl;

import com.mall4j.cloud.api.product.vo.AttrCategoryVO;
import com.mall4j.cloud.api.product.vo.AttrVO;
import com.mall4j.cloud.api.product.vo.AttrValueVO;
import com.mall4j.cloud.api.product.vo.CategoryVO;
import com.mall4j.cloud.common.database.dto.PageDTO;
import com.mall4j.cloud.common.database.util.PageUtil;
import com.mall4j.cloud.common.database.vo.PageVO;
import com.mall4j.cloud.product.dto.AttrCategoryDTO;
import com.mall4j.cloud.product.dto.AttrDTO;
import com.mall4j.cloud.product.dto.AttrValueDTO;
import com.mall4j.cloud.product.mapper.AttrCategoryMapper;
import com.mall4j.cloud.product.mapper.AttrMapper;
import com.mall4j.cloud.product.mapper.AttrValueMapper;
import com.mall4j.cloud.product.mapper.CategoryMapper;
import com.mall4j.cloud.product.model.Attr;
import com.mall4j.cloud.product.service.AttrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 属性
 *
 * @author JCM
 * @date 2023-04-23 09:25:00
 */
@Service
public class AttrServiceImpl implements AttrService {

    @Autowired
    private AttrMapper attrMapper;

    @Autowired
    private AttrCategoryMapper attrCategoryMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public PageVO<AttrVO> page(PageDTO pageDTO, AttrDTO attrDTO) {
        return PageUtil.doPage(pageDTO, () -> list(attrDTO));
    }

    @Override
    public List<AttrVO> list(AttrDTO attrDTO) {
        List<AttrVO> list = attrMapper.list(attrDTO);
        setAttrValue(list);
        return list;
    }

    @Override
    public AttrVO getByAttrId(Long attrId) {

        AttrVO attrVO = attrMapper.getByAttrId(attrId);

        //设置attrVO的其余字段
        //只有基本属性才与分类绑定
        if(attrVO.getAttrType() == 1) {
            List<CategoryVO> categoryVOS = new ArrayList<>();
            AttrCategoryDTO attrCategoryDTO = new AttrCategoryDTO();
            attrCategoryDTO.setAttrId(attrId);
            List<AttrCategoryVO> attrCategoryVOS = attrCategoryMapper.list(attrCategoryDTO);
            for (AttrCategoryVO attrCategoryVO : attrCategoryVOS) {
                List<String> pathNames = new ArrayList<>();
                CategoryVO categoryVO = categoryMapper.getByCategoryId(attrCategoryVO.getCategoryId());
                //找到它的所有父类
                String path = categoryVO.getPath();
                String[] split = path.split("-");
                for (int i = 1; i < split.length; i++) {
                    long ancestorId = Long.parseLong(split[i]);
                    CategoryVO ancestorCategoryVO = categoryMapper.getByCategoryId(ancestorId);
                    pathNames.add(ancestorCategoryVO.getName());
                }
                categoryVO.setPathNames(pathNames);
                categoryVOS.add(categoryVO);
            }
            attrVO.setCategories(categoryVOS);
        }

        AttrValueDTO attrValueDTO = new AttrValueDTO();
        attrValueDTO.setAttrId(attrId);
        List<AttrValueVO> attrValueVOS = attrValueMapper.list(attrValueDTO);
        attrVO.setAttrValues(attrValueVOS);

        return attrVO;
    }

    @Override
    public List<AttrVO> getAttrsByCategoryId(Long categoryId, Integer attrType) {
        //先根据categoryId找到所有对应属性，再对比它们的属性类别即可
        List<AttrVO> attrVOS = new ArrayList<>();
        List<AttrCategoryVO> attrCategoryVOS = attrCategoryMapper.getByCategoryId(categoryId);
        for (AttrCategoryVO attrCategoryVO : attrCategoryVOS){
            AttrVO attrVO = attrMapper.getByAttrId(attrCategoryVO.getAttrId());
            if(attrVO.getAttrType() == attrType){
                attrVOS.add(attrVO);
            }
        }

        setAttrValue(attrVOS);

        return attrVOS;
    }

    @Override
    public void save(Attr attr) {
        attrMapper.save(attr);
    }

    @Override
    public void update(Attr attr) {
        attrMapper.update(attr);
    }

    @Transactional
    @Override
    public void deleteById(Long attrId,Long shopId){
        //删除属性时，顺便删除与属性相关联其余表的数据
        attrCategoryMapper.deleteByAttrId(attrId);
        attrValueMapper.deleteByAttrId(attrId);
        attrMapper.deleteById(attrId,shopId);
    }

    /** 设置属性值 */
    private void setAttrValue(List<AttrVO> attrVOS){
        //设置属性对应的属性值
        for (AttrVO av : attrVOS ) {
            AttrValueDTO attrValueDTO = new AttrValueDTO();
            attrValueDTO.setAttrId(av.getAttrId());
            List<AttrValueVO> attrValueVOS = attrValueMapper.list(attrValueDTO);
            av.setAttrValues(attrValueVOS);
        }
    }
}
